package uk.org.scuts.bantstadium2;

import java.util.ArrayList;

import org.w3c.dom.Document;

import android.os.Bundle;
import android.os.Handler;
import android.os.Process;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class ActivityQuickBattle extends Activity {

	// Methods
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.quick_battle_main);
        charCfg = Utils.ParseXML(Utils.OpenXML(this, Utils.CustomCharsFilename(), Utils.CharsFilename()));
        SetupMenu();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	menu.add(0,  1,  1, "Help");
    	menu.add(0, 99, 99, "Exit");
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
    	switch(item.getItemId())
    	{
    	case 1:
    		Intent help = new Intent(this, ActivityHelp.class);
    		this.startActivity(help);
    		return true;
    	case 99:
    		Process.killProcess(Process.myPid());
    		return true;
    	}
    	return super.onOptionsItemSelected(item);
    }
    
	private void OneRound() {
    	if(_battle != null)
    	{
    		if (!_battle.Player().IsAlive())
    		{
    			Toast.makeText(this, R.string.youAreDead, Toast.LENGTH_SHORT).show();
    		} 
    		else if (!_battle.Opponent().IsAlive())
    		{
    			Toast.makeText(this, R.string.theyAreDead, Toast.LENGTH_SHORT).show();
    		}
    		else if (goodToGo)
    		{
        		goodToGo = false;
    			_battle.RunRound();
    		}	
    	}
    }

	
    public void SetupMenu() {
    	ArrayList<String> NameList = Utils.GetNameList(charCfg);
    	playerName = "";
    	opponentName = "";
    	Spinner playerSpin = (Spinner) findViewById(R.id.player_select);
        ArrayAdapter<String> playerAdapter = 
        	new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, NameList);
        playerSpin.setAdapter(playerAdapter);
        Spinner opponentSpin = (Spinner) findViewById(R.id.opponent_select);
        ArrayAdapter<String> opponentAdapter = 
        	new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, NameList);
        opponentAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        opponentSpin.setAdapter(opponentAdapter);
    }
    
    // Button listeners
    public void StartFight (View view)
    {
    	if (playerName.equals(""))
    	{
    		playerName = ((Spinner)findViewById(R.id.player_select)).getSelectedItem().toString();
            opponentName = ((Spinner)findViewById(R.id.opponent_select)).getSelectedItem().toString();
    	}
        setContentView(R.layout.battle);
    	battleHandler = new Handler();
        _battle = new Battle(this,playerName,opponentName);
        battleHandler.post(battleSetup);
    }
    
    public void NewFight (View view)
    {
    	setContentView(R.layout.quick_battle_main);
    	SetupMenu();
    }
    
    public void Bye (View view)
    {
    	finish();
    }
    
    public void SetAttack0 (View view)
    {
    	userAttackIdx = 0;
    	OneRound();
    }
    
    public void SetAttack1 (View view)
    {
    	userAttackIdx = 1;
    	OneRound();
    }
    
    public void SetAttack2 (View view)
    {
    	userAttackIdx = 2;
    	OneRound();
    }
    
    public void SetAttack3 (View view)
    {
    	userAttackIdx = 3;
    	OneRound();
    }
    
    // Members
    Handler battleHandler;
    private Battle _battle;
    static int userAttackIdx;
    static Boolean goodToGo = false;
    String playerName;
    String opponentName;
    Document charCfg;
    ArrayList<String> NameList;
 
    // Runnables
    Runnable battleUpdate = new Runnable() {
    	public void run() {
    		if (_battle.Player().IsAlive() && _battle.Opponent().IsAlive())
    		{
    			goodToGo = true;
    			battleHandler.post(battleUpdate);
    		}
    	}
    };
    
    Runnable battleSetup = new Runnable() {
    	public void run() {
    		
    		TextView log = (TextView)findViewById(R.id.battleLog);
            log.setText("FIGHT!");
            
    		TextView playerName = (TextView)findViewById(R.id.playerName);
            playerName.setText(_battle.Player().Name());
            playerName.setTextColor(_battle.Player().NameColour());
            TextView opponentName = (TextView)findViewById(R.id.opponentName);
            opponentName.setText(_battle.Opponent().Name());
            opponentName.setTextColor(_battle.Opponent().NameColour());
            
            TextView playerHp = (TextView)findViewById(R.id.playerHp);
            playerHp.setText(_battle.Player().HP());
            TextView opponentHp = (TextView)findViewById(R.id.opponentHp);
            opponentHp.setText(_battle.Opponent().HP());
            
            ProgressBar playerBar = (ProgressBar)findViewById(R.id.playerBar);
            playerBar.setVisibility(View.VISIBLE);
            playerBar.setProgress(_battle.Player().PercentHP());
            ProgressBar opponentBar = (ProgressBar)findViewById(R.id.opponentBar);
            opponentBar.setVisibility(View.VISIBLE);
            opponentBar.setProgress(_battle.Opponent().PercentHP());
            
            TextView attack0 = (TextView)findViewById(R.id.attack0);
            attack0.setText(_battle.Player()._attacks[0].Name());
            TextView attack1 = (TextView)findViewById(R.id.attack1);
            attack1.setText(_battle.Player()._attacks[1].Name());
            TextView attack2 = (TextView)findViewById(R.id.attack2);
            attack2.setText(_battle.Player()._attacks[2].Name());
            TextView attack3 = (TextView)findViewById(R.id.attack3);
            attack3.setText(_battle.Player()._attacks[3].Name());
            
            battleHandler.postDelayed(battleUpdate, 100);
    	}
    };
    
}
